package com.kedacom.freedomLabel.view.activity.printer;

import android.os.RemoteException;
import android.util.Base64;

//import com.gprinter.aidl.GpService;
import com.gprinter.command.EscCommand;
import com.gprinter.command.GpUtils;
import com.gprinter.command.LabelCommand;
import com.kedacom.freedomLabel.base.UserCache;
import com.kedacom.freedomLabel.model.bean.Business;
import com.kedacom.freedomLabel.model.bean.Config;
import com.kedacom.freedomLabel.model.bean.ConfigKeys;
import com.kedacom.freedomLabel.model.bean.PrintShowBean;
import com.kedacom.freedomLabel.model.bean.Template;
import com.kedacom.freedomLabel.model.db.dao.ConfigDao;
import com.kedacom.freedomLabel.utils.ExcelUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Vector;

public class LablePrint {

    private static SimpleDateFormat sdf1= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static String getUUValue(int i, Business business) {

        try {
            if (i==0) {
                return  business.itemValue1;
            } else if (i==1) {
                return  business.itemValue2;
            } else if (i==2) {
                return  business.itemValue3;
            } else if (i==3) {
                return  business.itemValue4;
            } else if (i==4) {
                return  business.itemValue5;
            } else if (i==5) {
                return  business.itemValue6;
            }
            return  business.itemValue1;
        }catch (Exception e){
            e.printStackTrace();
            return  business.itemValue1;
        }

    }


//    public static int printSixToFour(int serial_number,Business business, int printId, GpService mGpService,
//                                     Template template, String direction, String[] columnNames, Boolean[] booleansNames, ConfigDao dao ) throws RemoteException {

    public static int printSixToFour(int serial_number,Business business, int printId,
                                     Template template, String direction, String[] columnNames, Boolean[] booleansNames, ConfigDao dao ) throws RemoteException {

        List<PrintShowBean> printShowBeanList =new ArrayList<>();
        if(columnNames!=null && booleansNames!=null ){
            printShowBeanList.clear();
            printShowBeanList = Assemblyprinting(business, columnNames, booleansNames);
        }

        LabelCommand.DIRECTION directionSet = LabelCommand.DIRECTION.FORWARD;
        int width = 60;
        int height = 40;
        width = template.width;
        height = template.height;

        if (direction.equals("1")) {
            directionSet = LabelCommand.DIRECTION.BACKWARD;
        }

        int resultId = -1;
        LabelCommand tsc = new LabelCommand();
        tsc.addSize(width, height); // 设置标签尺寸，按照实际尺寸设置
        tsc.addGap(2); // 设置标签间隙，按照实际尺寸设置，如果为无间隙纸则设置为0
        tsc.addDirection(directionSet, LabelCommand.MIRROR.NORMAL);// 设置打印方向
        tsc.addReference(0, 0);// 设置原点坐标
        tsc.addTear(EscCommand.ENABLE.ON); // 撕纸模式开启
        tsc.addSpeed(LabelCommand.SPEED.SPEED2);
        tsc.addCls();// 清除打印缓冲区

        if(printShowBeanList.size()>0){
            for (int j= 0;j<2;j++) {
                String showuuName ="";
                if(printShowBeanList.get(j).getUuNameValue()!=null){
                    showuuName = printShowBeanList.get(j).getUuNameValue();
                }
                if(j==0){
                    tsc.addText(20, 30+40*j, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                            showuuName);
                }
                if(j==1){
                    tsc.addText(20, 30+40*j, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                            printShowBeanList.get(j).getUuName() +":"+showuuName);
                }

//                 tsc.addText(20, 20+30*j, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
//                         printShowBeanList.get(j).getUuName()+":" +showuuName);

            }
        }

        tsc.addText(20, 110, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                "类别:" + business.name);
        tsc.addText(20, 150, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                "重量:" + business.netWeight+business.weightUnit);
        tsc.addText(20, 190, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                "操作人:" + business.operator_name);
        tsc.addText(20, 230, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                "序号:" + serial_number+"");
        tsc.addText(20, 270, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                "时间:" +sdf1.format(System.currentTimeMillis()));


        tsc.addQRCode(250, 30, LabelCommand.EEC.LEVEL_H, 6, LabelCommand.ROTATION.ROTATION_0,
                serial_number+"$"+business.name+
                        "$"+business.itemUUID1+
                        "$"+business.itemUUID2+
                        "$"+business.netWeight+business.weightUnit.trim()+
                        "$"+sdf1.format(System.currentTimeMillis())+
                        "$"+serial_number+System.currentTimeMillis());


        tsc.addPrint(1, 1); // 打印标签
        tsc.addSound(2, 100); // 打印标签后 蜂鸣器响
//        resultId = getResultId(printId, mGpService, tsc);
        return resultId;
    }

    private static  List<PrintShowBean>  Assemblyprinting(Business business, String[] columnNames, Boolean[] booleansNames) {
        List<PrintShowBean> printShowBeanList =new ArrayList<>();
        printShowBeanList.clear();
        for (int i = 0; i < booleansNames.length; i++) {
            PrintShowBean printShowBean = new PrintShowBean();
            if (booleansNames[i]) {
                printShowBean.setUuName(columnNames[i]);
                String UuNameValueStr = getUUValue(i, business);
                printShowBean.setUuNameValue(UuNameValueStr);
                printShowBeanList.add(printShowBean);
            }
        }
        return printShowBeanList;
    }
//    打印格式如上：
//    医院名称、科室、类别、重量、操作员、序号、日期、时间；
//
//    二维码内容：
//    称重序号$称重类别$医院编号$科室编号$称重重量$称重时间
//            $流水号=称重序号+称重时间

// public static int printSixToThree(int serial_number,Business business, int printId, GpService mGpService,
//                Template template, String direction, String[] columnNames, Boolean[] booleansNames, ConfigDao dao ) throws RemoteException {
 public static int printSixToThree(int serial_number,Business business, int printId,
                Template template, String direction, String[] columnNames, Boolean[] booleansNames, ConfigDao dao ) throws RemoteException {

            List<PrintShowBean> printShowBeanList =new ArrayList<>();
            if(columnNames!=null && booleansNames!=null ){
                printShowBeanList.clear();
                printShowBeanList = Assemblyprinting(business, columnNames, booleansNames);
            }

            LabelCommand.DIRECTION directionSet = LabelCommand.DIRECTION.FORWARD;
            int width = 60;
            int height = 40;
            width = template.width;
            height = template.height;

            if (direction.equals("1")) {
                directionSet = LabelCommand.DIRECTION.BACKWARD;
            }

            int resultId = -1;
            LabelCommand tsc = new LabelCommand();
            tsc.addSize(width, height); // 设置标签尺寸，按照实际尺寸设置
            tsc.addGap(2); // 设置标签间隙，按照实际尺寸设置，如果为无间隙纸则设置为0
            tsc.addDirection(directionSet, LabelCommand.MIRROR.NORMAL);// 设置打印方向
            tsc.addReference(0, 0);// 设置原点坐标
            tsc.addTear(EscCommand.ENABLE.ON); // 撕纸模式开启
            tsc.addSpeed(LabelCommand.SPEED.SPEED2);
            tsc.addCls();// 清除打印缓冲区


//            if( dao.get(ConfigKeys.COMPANY_NAME).size()>0 && dao.get(ConfigKeys.COMPANY_NAME).get(0)!=null){
//                tsc.addText(20, 20, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
//                        dao.get(ConfigKeys.COMPANY_NAME).get(0).configValue +
//                                dao.get(ConfigKeys.COMPANY_ADDRESS).get(0).configValue+
//                                dao.get(ConfigKeys.COMPANY_CONTACT_NUMBER).get(0).configValue);
//            }else {
//                tsc.addText(20, 20, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
//                        "没有设置基本信息");
//            }

         if(printShowBeanList.size()>0){
             for (int j= 0;j<2;j++) {
                 String showuuName ="";
                 if(printShowBeanList.get(j).getUuNameValue()!=null){
                     showuuName = printShowBeanList.get(j).getUuNameValue();
                 }
                 if(j==0){
                     tsc.addText(20, 20+30*j, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                             showuuName);
                 }
                 if(j==1){
                     tsc.addText(20, 20+30*j, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                             printShowBeanList.get(j).getUuName() +":"+showuuName);
                 }

//                 tsc.addText(20, 20+30*j, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
//                         printShowBeanList.get(j).getUuName()+":" +showuuName);

             }
         }

            tsc.addText(20, 80, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                    "类别:" + business.name);
            tsc.addText(20, 110, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                 "重量:" + business.netWeight+business.weightUnit);
            tsc.addText(20, 140, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                    "操作人:" + business.operator_name);
            tsc.addText(20, 170, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                 "序号:" + serial_number+"");
            tsc.addText(20, 200, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE, LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
                 "时间:" +sdf1.format(System.currentTimeMillis()));


            tsc.addQRCode(280, 20, LabelCommand.EEC.LEVEL_M, 5, LabelCommand.ROTATION.ROTATION_0,
                    serial_number+"$"+business.name+
                    "$"+business.itemUUID1+
                    "$"+business.itemUUID2+
                    "$"+business.netWeight+business.weightUnit.trim()+
                            "$"+sdf1.format(System.currentTimeMillis())+
                            "$"+serial_number+System.currentTimeMillis());


            tsc.addPrint(1, 1); // 打印标签
            tsc.addSound(2, 100); // 打印标签后 蜂鸣器响
//            resultId = getResultId(printId, mGpService, tsc);
            return resultId;

    }
//    private static int getResultId(int printId, GpService mGpService, LabelCommand tsc) throws RemoteException {
    private static int getResultId(int printId,  LabelCommand tsc) throws RemoteException {
        int resultId=0;
        Vector<Byte> datas = tsc.getCommand(); // 发送数据
        byte[] bytes = GpUtils.ByteTo_byte(datas);
        String packStr = Base64.encodeToString(bytes, Base64.DEFAULT);
//        resultId = mGpService.sendLabelCommand(printId, packStr);
        return resultId;
    }
}
